package com.bolang.onlycode.demo.controller;

import com.bolang.onlycode.demo.activemq.Producer;
import com.bolang.onlycode.demo.annoation.MyFirstAnnotation;
import com.bolang.onlycode.demo.aop.AdviceTestImpl;
import com.bolang.onlycode.demo.aop.IAdviceTest;
import com.bolang.onlycode.demo.configuratin.TestAsyncConfig;
import com.bolang.onlycode.demo.dynamicDatasource.People;
import com.bolang.onlycode.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

@RestController
@RequestMapping("/sys")
public class TestController  {
    @Value("${key}")
    String key;
    @Autowired
    AdviceTestImpl adviceTest;
    @Autowired
    TestAsyncConfig asyncConfig;
    @Autowired
    private Producer producer;
    @Resource
    @Qualifier("threadPoolPrintExecutor")
    Executor threadPoolTaskExecutor;
    @Autowired
    TestService testService;
    @Autowired
    private People people;
    @GetMapping("/get/{name}")
    @MyFirstAnnotation(operateType = 1)
    public String getTest(@PathVariable("name") String name){
//        adviceTest.afterAdviceTest();
        CompletableFuture.supplyAsync(()->testService.hello(),asyncConfig.getAsyncExecutor());
        threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"hello2");
            }
        });
        System.out.println(Thread.currentThread().getName()+"hello1");
        return people.getName();
    }
    @GetMapping("/update/{name}")
    @MyFirstAnnotation(operateType = 2)
    public String updateTest(@PathVariable("name") String name){
        System.out.println(key);
        System.out.println(Thread.currentThread().getName());
        return name;
    }
    @GetMapping("/add/{name}")
    @MyFirstAnnotation(operateType = 3)
    public String addTest(@PathVariable("name") String name){
//        System.out.println(Thread.currentThread().getName());
//        return name;
        producer.sendTopic(name);
        return name;
    }
    @GetMapping("/delete/{name}")
    @MyFirstAnnotation(operateType = 4)
    public String deleteTest(@PathVariable("name") String name){
//        System.out.println(Thread.currentThread().getName());
//        return name;
        producer.sendQueue(name);
        return name;
    }
//    @GetMapping("/test/{name}")
//    @MyFirstAnnotation(operateType = 1)
//    @Override
//    public void afterAdviceTest(@PathVariable("name") String name) {
//        System.out.println(Thread.currentThread().getName());
//        adviceTest.afterAdviceTest(name);
//
//    }
//    @GetMapping("/test1/{name}")
//    @Override
//    public void afterReturningAdviceTest() {
//
//    }
//    @GetMapping("/test2/{name}")
//    @Override
//    public void allAdviceExecutionOrderTest() {
//
//    }
//    @GetMapping("/test3/{name}")
//    @Override
//    public void allAdviceExecutionContainExceptionOrderTest() {
//
//    }
}
